package io.renren.modules.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.pet.dao.PetLostCommentDao;
import io.renren.modules.pet.entity.PetLostCollectEntity;
import io.renren.modules.pet.vo.PetLostCommentVO;
import io.renren.modules.pet.dto.PetLostCommentDTO;
import io.renren.modules.pet.entity.PetLostCommentEntity;
import io.renren.modules.pet.service.PetLostCommentService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 丢失宠物评论表
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-21
 */
@Service
public class PetLostCommentServiceImpl extends CrudServiceImpl<PetLostCommentDao, PetLostCommentEntity, PetLostCommentVO> implements PetLostCommentService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<PetLostCommentEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PetLostCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void save(PetLostCommentDTO petLostCommentDTO) {
        PetLostCommentEntity petLostCommentEntity = ConvertUtils.sourceToTarget(petLostCommentDTO, PetLostCommentEntity.class);
        this.insert(petLostCommentEntity);
    }

    @Override
    public void update(PetLostCommentDTO petLostCommentDTO) {
        PetLostCommentEntity petLostCommentEntity = ConvertUtils.sourceToTarget(petLostCommentDTO, PetLostCommentEntity.class);
        this.updateById(petLostCommentEntity);
    }

    @Override
    public List<PetLostCommentVO> getPetLostComment(Long petLostId) {
        QueryWrapper<PetLostCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_lost_id", petLostId);
        wrapper.orderByDesc("create_date");
        List<PetLostCommentEntity> entityList = this.baseDao.selectList(wrapper);
        List<PetLostCommentVO> voList = ConvertUtils.sourceToTarget(entityList, PetLostCommentVO.class);
        voList.forEach(e -> {
            SysUserDTO userDto = sysUserService.get(e.getCreator());
            e.setCreatorName(userDto == null ? "" : userDto.getRealName());
        });
        return voList;
    }

    @Override
    public void delByPetLostId(Long petLostId) {
        QueryWrapper<PetLostCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_lost_id", petLostId);
        this.baseDao.delete(wrapper);
    }

}